package com.davy.gridpoint.qlxj.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.davy.gridpoint.R;
import com.davy.gridpoint.http.HttpClient;
import com.davy.gridpoint.utils.StringUtils;

import java.util.List;
import java.util.Map;

public class JcbhAdapter extends RecyclerView.Adapter<JcbhAdapter.ItemViewHolder> {
    private ItemClickListener mItemClick;
    private Context mContext;
    private List<Map> mList;

    public JcbhAdapter(Context mContext, List<Map> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setData(List<Map> vList) {
        this.mList = vList;
        notifyDataSetChanged();
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClick = listener;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder = new ItemViewHolder(LayoutInflater
                .from(mContext).inflate(R.layout.item_jcbh, parent, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        if (mList.size() > position) {
            holder.imgZe.setVisibility(View.GONE);
            holder.imgAdd.setVisibility(View.VISIBLE);
            String img = mList.get(position).get("img") + "";
            Double isBt = 1.0;//1：保存 2：提交
            if (mList.get(position).containsKey("isBt")) {
                isBt = Double.valueOf(mList.get(position).get("isBt") + "");
            }

            if (isBt.intValue() == 2) {
                if (!img.startsWith("http")) {
                    img = HttpClient.imgUrl + img.replace("/storage/emulated/0/Davy/", "");
                }
                holder.imgDel.setVisibility(View.GONE);
            } else {
                holder.imgDel.setVisibility(View.VISIBLE);
            }
            int type = Double.valueOf(StringUtils.isEmpty(mList.get(position).get("type") + "") ? "0" : mList.get(position).get("type") + "").intValue();

            if (type == 1 || type == 2) {
                Glide.with(mContext).load(img).error(R.mipmap.icon_error).into(holder.imgAdd);
            }

            if (type == 2) {
                holder.imgZe.setVisibility(View.VISIBLE);
                holder.imgZe.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
                holder.imgZe.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_bof));
            }
            if (type == 3) {
                holder.imgAdd.setVisibility(View.GONE);
                holder.imgZe.setVisibility(View.VISIBLE);
                holder.imgZe.setBackground(mContext.getResources().getDrawable(R.drawable.bg_dialog_input));
                holder.imgZe.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_lab));
            }

            holder.imgDel.setOnClickListener(v -> {
                if (mItemClick != null) {
                    mItemClick.delClick(position, mList.get(position));
                }
            });

            holder.imgAdd.setOnClickListener(v -> {
                if (mItemClick != null) {
                    mItemClick.getClick(position, mList.get(position));
                }
            });
            holder.imgZe.setOnClickListener(v -> {
                if (mItemClick != null) {
                    mItemClick.getClick(position, mList.get(position));
                }
            });
        } else {
            holder.imgDel.setVisibility(View.GONE);
            holder.imgZe.setVisibility(View.GONE);
            holder.imgAdd.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_tj));
            holder.imgAdd.setOnClickListener(v -> {
                if (mItemClick != null) {
                    mItemClick.addClick(position);
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (mList.size() == 8) {
            return mList.size();
        } else {
            return mList.size() + 1;
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView imgAdd, imgDel, imgZe;

        public ItemViewHolder(View itemView) {
            super(itemView);
            imgZe = itemView.findViewById(R.id.imgZe);
            imgAdd = itemView.findViewById(R.id.imgAdd);
            imgDel = itemView.findViewById(R.id.imgDel);
        }
    }

    public interface ItemClickListener {
        void addClick(int position);

        void delClick(int position, Map mList);

        void getClick(int position, Map mList);
    }
}
